package com.guochu.common.infrastructure;

import com.guochu.common.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 数据表基础字段赋值
 *
 * <p>当添加和修改数据时，给数据的用户信息字段赋值
 * ID、CreateUserId、CreateUserName、CreateDate
 * 此类只是为了方便开发人员编写代码时，减少重复赋值操作，节约代码量
 *
 * @author bunting.wei
 * @since 2020-12-11 15:48:35
 */
@Service
public class IService {

    @Autowired
    protected AuthService authService;

    protected IEntity create(IEntity ientity) {
        ientity.setId(UUID.randomUUID().toString());
        Identity identity = authService.getIdentity();
        if (null != identity) {
            ientity.setCreateUserId(identity.getUser().getId());
            ientity.setCreateUserName(identity.getUser().getUserName());
        }
        ientity.setCreateDate(LocalDateTime.now());
        ientity.setDeleteMark(0);
        ientity.setEnabledMark(1);
        return ientity;
    }

}
